package com.sudu.controller;

import com.alibaba.fastjson.JSONObject;
import com.sudu.domain.Comment;
import com.sudu.service.CommentService;
import com.sudu.service.VideoService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Api
public class CommentController {
    @Autowired
    private CommentService commentService ;
    @Autowired
    private VideoService videoService ;

//    @Autowired
//    private UserService userService ;

    @PostMapping("/comment_post/{uid}")
    public Result saveComment( @PathVariable int uid , @RequestBody JSONObject obj) {
        Comment comment = new Comment() ;
        comment.setContent(obj.getString("commentContent"));
        comment.setCreateTime(new Date());
        comment.setVideoId(obj.getInteger("articleId"));
        comment.setParentId(obj.getInteger("parentId"));
        if (obj.getString("parentName") != null)  {
            comment.setParentNickname(obj.getString("parentName"));
        }
        commentService.saveComment(comment , uid ) ;
        videoService.comment(obj.getInteger("articleId")) ;
        return new Result(Code.SAVE_OK) ;
    }

    @GetMapping("/comment/{vid}")
    public Result getCommentByVid( @PathVariable int vid) {
        List<Comment> comments = commentService.listComment(vid) ;
        return new Result(Code.GET_OK , comments) ;
    }

}
